from django.db import models
from django.utils.html import format_html
from django.utils.safestring import mark_safe


class Admin(models.Model):
    id = models.BigAutoField(primary_key=True)
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'admin'


class AuthGroup(models.Model):
    name = models.CharField(unique=True, max_length=150)

    class Meta:
        managed = False
        db_table = 'auth_group'


class AuthGroupPermissions(models.Model):
    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
    permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_group_permissions'
        unique_together = (('group', 'permission'),)


class AuthPermission(models.Model):
    name = models.CharField(max_length=255)
    content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
    codename = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'auth_permission'
        unique_together = (('content_type', 'codename'),)


class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'


class AuthUserGroups(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_user_groups'
        unique_together = (('user', 'group'),)


class AuthUserUserPermissions(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_user_user_permissions'
        unique_together = (('user', 'permission'),)


class DjangoAdminLog(models.Model):
    action_time = models.DateTimeField()
    object_id = models.TextField(blank=True, null=True)
    object_repr = models.CharField(max_length=200)
    action_flag = models.PositiveSmallIntegerField()
    change_message = models.TextField()
    content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True)
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'django_admin_log'


class DjangoContentType(models.Model):
    app_label = models.CharField(max_length=100)
    model = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'django_content_type'
        unique_together = (('app_label', 'model'),)


class DjangoMigrations(models.Model):
    app = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    applied = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'django_migrations'


class DjangoSession(models.Model):
    session_key = models.CharField(primary_key=True, max_length=40)
    session_data = models.TextField()
    expire_date = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'django_session'


class Floor(models.Model):
    id = models.BigAutoField(primary_key=True)
    library = models.ForeignKey('Library', models.DO_NOTHING)
    floor_number = models.IntegerField(blank=True, null=True)
    open_status = models.IntegerField(blank=True, null=True)
    x = models.IntegerField(blank=True, null=True)
    y = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'floor'


class Layout(models.Model):
    id = models.BigAutoField(primary_key=True)
    row = models.IntegerField(blank=True, null=True)
    field_column = models.IntegerField(db_column=' column', blank=True,
                                       null=True)  # Field renamed to remove unsuitable characters. Field renamed because it started with '_'.

    class Meta:
        managed = False
        db_table = 'layout'


class Library(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=20, blank=True, null=True)
    floors = models.IntegerField(blank=True, null=True)
    campus = models.CharField(max_length=20, blank=True, null=True)
    book_stock = models.CharField(max_length=255, blank=True, null=True)
    open_time = models.TimeField(blank=True, null=True)
    close_time = models.TimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'library'


class Seat(models.Model):
    id = models.BigAutoField(primary_key=True)
    floor = models.ForeignKey(Floor, models.DO_NOTHING, blank=True, null=True)
    number = models.CharField(max_length=20, blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'seat'


class SeatSelection(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey('User', models.DO_NOTHING)
    seat = models.ForeignKey(Seat, models.DO_NOTHING)
    start = models.DateTimeField()
    end = models.DateTimeField()
    delete_status = models.IntegerField(blank=True, null=True)
    return_status = models.IntegerField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'seat_selection'


class TipOffs(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey('User', models.DO_NOTHING, verbose_name='举报用户', blank=True, null=True)
    create_time = models.DateTimeField("举报时间", blank=True, null=True)
    note = models.CharField("内容", max_length=255, blank=True, null=True)
    handle_status = models.IntegerField("处理状态", blank=True, null=True)
    repay = models.CharField("管理员处理回复", max_length=255, blank=True, null=True)
    handle_time = models.DateTimeField("处理时间", blank=True, null=True)
    seat = models.ForeignKey(Seat, models.DO_NOTHING, blank=True, null=True)
    position = models.CharField("位置", max_length=32, blank=True, null=True)
    seat_number = models.CharField("座位号", max_length=11, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tip_offs'
        verbose_name = '举报管理'
        verbose_name_plural = '举报处理'

    def get_handle_status(self):
        if self.handle_status == 0:
            return mark_safe('<span style="color: red;">{}</span>'.format("未处理"))
        return mark_safe('<span style="color: green;">{}</span>'.format("已处理"))

    get_handle_status.short_description = "状态"

    def __str__(self):
        return str(self.position) + str(self.seat_number)


class User(models.Model):
    id = models.BigAutoField('用户编号', primary_key=True)
    number = models.CharField('学号/工号', max_length=20)
    name = models.CharField('姓名', max_length=40, blank=True, null=True)
    sex = models.CharField('性别', max_length=10, blank=True, null=True)
    type = models.IntegerField('用户类型:1学生，2教职人员，3员工', blank=True, null=True)
    password = models.CharField('密码', max_length=255, blank=True, null=True)
    phone = models.CharField('联系方式', max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user'
        verbose_name = '用户管理'
        verbose_name_plural = '用户管理'

    def __str__(self):
        return str(self.name) + "[" + str(self.number) + "]"
